<?php
require_once('../classes/smarty_setup.php');
require_once('../classes/mofachi.Baby.php');
$smarty = new Smarty_mofachi();

@ $act = $_REQUEST[ "act" ];
if( $act == "add" ) {
	addBabyinfo();
} else if( $act == "remove" ) {
	removeBabyinfo();
} else if( $act == "get" ) {
	getBabyinfo( $smarty );
} else if( $act == "update" ) {
	updateBabyinfo();
}
$smarty->assign( 'act', $act );

listBabyinfo( $smarty );
$smarty->display('t03_babyinfo.htm');

function listBabyinfo( $smarty ) {
	$result = Baby::listBaby( 0, 20 );
	$smarty->assign( 'res', $result );
}
function addBabyinfo() {
	@ $nickname = $_POST[ 'nickname' ];
	@ $realname = $_POST[ 'realname' ];
	@ $gender = $_POST[ 'gender' ];
	@ $location_id = $_POST[ 'location_id' ];
	@ $birthday = $_POST[ 'birthday' ];
	if( !$nickname )
		return;

	$baby = new Baby();
	$baby->nickname = $nickname;
	$baby->realname = $realname;
	$baby->gender = $gender;
	$baby->location_id = $location_id;
	$baby->birthday = $birthday;
	$baby->add();
}
function removeBabyinfo() {
	@ $delist = $_POST[ "delist" ];
	if( !$delist )
		return;

	$id_array = explode( ",", $delist );
	foreach( $id_array as $key => $value ) {
		$baby = new Baby();
		$baby->baby_id = $value;
		$baby->remove();
	}
}
function getBabyinfo( $smarty ) {	
	@ $bid = $_GET[ "bid" ];
	if( !$bid )
		return;
	
	$baby = new Baby();
	$baby->baby_id = $bid;
	$baby->loadById();
	$smarty->assign( 'row', $baby );
}
function updateBabyinfo() {
	@ $bid = $_POST[ 'bid' ];
	@ $nickname = $_POST[ 'nickname' ];
	@ $realname = $_POST[ 'realname' ];
	@ $gender = $_POST[ 'gender' ];
	@ $location_id = $_POST[ 'location_id' ];
	@ $birthday = $_POST[ 'birthday' ];

	if( !$bid || !$nickname )
		return;
	
	$baby = new Baby();
	$baby->baby_id = $bid;
	$baby->loadById();
	$baby->nickname = $nickname;
	$baby->realname = $realname;
	$baby->gender = $gender;
	$baby->location_id = $location_id;
	$baby->birthday = $birthday;
	$baby->update();
}
?>